#ifndef _SINGLE_HPP
#define _SINGLE_HPP
#include <pthread.h>

namespace com_oocl_riley
{
template <typename T>
class Single
{
public:
    static T* getInstance();
protected:
    Single() {}
    ~Single() {}
private:
    static T* _instance;
    static pthread_mutex_t _mutex;
private:
    Single(const Single&);
    Single& operator=(const Single&);
};

template <typename T>
T* Single<T>::_instance=nullptr;

template <typename T>
pthread_mutex_t Single<T>::_mutex=PTHREAD_MUTEX_INITIALIZER;

template <typename T>
T* Single<T>::getInstance()
{
    if(_instance==nullptr)
    {
        pthread_mutex_lock(&_mutex);
        if(_instance==nullptr)
        {
            _instance= new T;
        }
        pthread_mutex_unlock(&_mutex);
    }
    return _instance;
}

}

#endif // _SINGLE_HPP
